import sys
import platform
import os
import psutil
import wx
import urllib2
numerocpus = psutil.NUM_CPUS
cpu = str(psutil.cpu_percent())
memoria = str(psutil.virtual_memory())
particiones = str(psutil.disk_usage('/'))
user = str(psutil.get_users())

#Operaciones sobre memoria
mtotal, mavailable, mpercent, mused, mfree, mactive, minactive, mwired = memoria.split(',')
rest, mtotal = mtotal.split('=')
mtotal, rest = mtotal.split('L')
mtotal = (int(mtotal) / 1024) / 1024
rest, mavailable = mavailable.split('=')
mavailable, rest = mavailable.split('L')
mavailable = (int (mavailable) / 1024) / 1024
rest, mpercent = mpercent.split('=')
rest, mused = mused.split('=')
mused, rest = mused.split('L')
mused = (int (mused) / 1024) / 1024
rest, mfree = mfree.split('=')
mfree, rest = mfree.split('L')
mfree = (int (mfree) / 1024) / 1024
rest, mactive = mactive.split('=')
rest, minactive = minactive.split('=')
rest, mwired = mwired.split('=')
mwired, rest = mwired.split(')')

#Operaciones sobre disco
dtotal, dused, dfree, dpercent = particiones.split(',')
rest, dtotal = dtotal.split('=')
dtotal, rest = dtotal.split('L')
dtotal = (int (dtotal) / 1024) / 1024
rest, dused = dused.split('=')
dused, rest = dused.split('L')
dused = (int (dused) / 1024) / 1024
rest, dfree = dfree.split('=')
dfree, rest = dfree.split('L')
dfree = (int (dfree) / 1024) / 1024
rest, dpercent = dpercent.split('=')
dpercent, rest = dpercent.split(')') #Quitamos los simbolos innecesarios.

#Operaciones sobre usuario
uname, rest = user.split(',', 1) #Split primario
rest, uname, rest2 = uname.split('\'') #Split secundario, sobran dos restos.
cpu = float(cpu)
dpercent = float(dpercent)

class Gogo(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="Pynventory", size=(420, 270))
        self.panel = wx.Panel(self)
        sistema = sys.platform
        issistema = sistema.startswith("darwin")
        if (issistema == True):
        	sistema = "Mac OS X"
        	unix = 1
        	sysicon = 'mac_os.png'
        else:
        	issistema = sistema.startswith("linux")
        	if (issistema == True):
        		sistema = "Linux"
        		unix = 1
        		sysicon = 'linux.png'
        	else:
        		issistema = sistema.startswith("win")
        		if (issistema == True):
        			sistema = "Windows" 
        			unix = 0
        			sysicon = 'windows.png'  
        		else:
        			issistema = sistema.startswith("freebsd")
        			if (issistema == True):
        				sistema = "FreeBSD"
        				unix = 1
        				sysicon = 'free_bsd.png'
        			else:
        				issistema = sistema.startswith("mac")
        				if (issistema == True):
        					sistema = "Mac OS X"
        					unix = 1
        					sysicon = 'mac_os.png'
        				else:
        					sistema = "Desconocido"
        					unix = 0
        					sysicon = 'unknown.png'
	
		
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox2 = wx.BoxSizer(wx.VERTICAL)
        self.display = wx.StaticText(self, label=" ")
        hbox.Add(self.display, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
        vbox.Add(hbox, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
        vbox.Add((-1, 10))
        
        if (numerocpus == 1):
        	coresicon = '1core.png'
        else:
        	coresicon = '2cores.png'

        	
        toolbar = self.CreateToolBar()
        asis = toolbar.AddLabelTool(wx.ID_ANY, 'OS', wx.Bitmap(sysicon))
        toolbar.AddSeparator()
        unixlike = toolbar.AddLabelTool(wx.ID_ANY, 'Unix-Like', wx.Bitmap('unix.png'))
        toolbar.AddSeparator()
        cores = toolbar.AddLabelTool(wx.ID_ANY, 'Cores', wx.Bitmap(coresicon))
        toolbar.AddSeparator()
        usage = toolbar.AddLabelTool(wx.ID_ANY, 'CPU usage', wx.Bitmap('usage.png'))
        toolbar.AddSeparator()
        hdd = toolbar.AddLabelTool(wx.ID_ANY, 'Disk usage', wx.Bitmap('hdd.png'))
        toolbar.AddSeparator()
        about = toolbar.AddLabelTool(wx.ID_ANY, 'About', wx.Bitmap('package.png'))
        if (cpu <= 50):
        	id = usage.GetId()
        	toolbar.EnableTool(id, False)
        
        if (unix == 0):
        	id = unixlike.GetId()
        	toolbar.EnableTool(id, False)

        if (dpercent <= 70):
	    	id = hdd.GetId()
	    	toolbar.EnableTool(id, False)

        toolbar.Realize()
        self.Bind(wx.EVT_TOOL, lambda event: self.OnSys(event, sistema), asis)
        self.Bind(wx.EVT_TOOL, self.OnUni, unixlike)
        self.Bind(wx.EVT_TOOL, lambda event: self.OnCor(event, sistema), cores)
        self.Bind(wx.EVT_TOOL, self.OnUsa, usage)
        self.Bind(wx.EVT_TOOL, self.OnHDD, hdd)
        self.Bind(wx.EVT_TOOL, self.OnAbout, about)

        
        gs = wx.GridSizer(9, 4, 0, 0)
        sistemax = wx.StaticText(self, label=" OS: ")
        sistemay = wx.TextCtrl(self)
        cpux = wx.StaticText(self, label=" Cpus: ")
        userx = wx.StaticText(self, label=" Main user: ")
        usery = wx.TextCtrl(self)
        cpuy = wx.TextCtrl(self)
        ucpux = wx.StaticText(self, label=" Cpu Usage: ")
        ucpuy = wx.TextCtrl(self)
        metotalx = wx.StaticText(self, label=" Total memory: ")
        metotaly = wx.TextCtrl(self)
        meavailx = wx.StaticText(self, label=" Avail. memory: ")
        meavaily = wx.TextCtrl(self)
        mepercentx = wx.StaticText(self, label=" Memory usage: ")
        mepercenty = wx.TextCtrl(self)
        meusedx = wx.StaticText(self, label=" Used memory: ")
        meusedy = wx.TextCtrl(self)
        mefreex = wx.StaticText(self, label=" Free memory: ")
        mefreey = wx.TextCtrl(self)
        ditotalx = wx.StaticText(self, label=" Total space: ")
        ditotaly = wx.TextCtrl(self)
        diusedx = wx.StaticText(self, label=" Used space: ")
        diusedy = wx.TextCtrl(self)
        difreex = wx.StaticText(self, label=" Free space: ")
        difreey = wx.TextCtrl(self)
        dipercentx = wx.StaticText( self, label=" Disk usage: ")
        dipercenty = wx.TextCtrl(self)
        
        
        gs.AddMany( [ 
        (userx), (usery), (sistemax), (sistemay), 
        (cpux), (cpuy), (ucpux), (ucpuy),
        (metotalx), (metotaly), (meavailx), (meavaily),
        (mepercentx), (mepercenty), (meusedx), (meusedy),
        (mefreex), (mefreey), (ditotalx), (ditotaly),
        (diusedx), (diusedy), (difreex), (difreey),
        (dipercentx), (dipercenty),
        
        ])
        
        usery.SetValue(str(uname))
        sistemay.SetValue(str(sistema))
        cpuy.SetValue(str(numerocpus))
        ucpuy.SetValue(str(cpu) + " %")
        metotaly.SetValue(str(mtotal) + " MB")
        meavaily.SetValue(str(mavailable) + " MB")
        mepercenty.SetValue(str(mpercent) + " %")
        meusedy.SetValue(str(mused) + " MB")
        mefreey.SetValue(str(mfree) + " MB")
        ditotaly.SetValue(str(dtotal) + " MB")
        diusedy.SetValue(str(dused) + " MB")
        difreey.SetValue(str(dfree) + " MB")
        dipercenty.SetValue(str(dpercent) + " %")

        vbox.Add(gs, proportion=1, flag=wx.EXPAND)
        self.SetSizer(vbox)
        
        
        

        
    def OnQuit(self, e):
        self.Close()
    def OnUni(self, e):
    	wx.MessageBox("This system is Unix-based.", "UNIX-Based", wx.OK)
    def OnHDD(self, e):
    	wx.MessageBox("Your main storage media is nearly full.", "Drive almost full", wx.OK)
    def OnUsa(self, e):
    	wx.MessageBox("Your CPU usage is becoming high.", "High CPU Usage", wx.OK)
    def OnCor(self, event, sistema):
    	wx.MessageBox("Your system has: " + str(numerocpus) + " cores", "CPU cores", wx.OK)
    def OnSys(self, event, sistema):
    	wx.MessageBox("OS: " + sistema, "Operating System", wx.OK)
    def OnAbout(self, e):
    	wx.MessageBox("Developed by MauroEldritch @ Eldritch Software - http://www.eldritch.com.ar", "About", wx.OK | wx.ICON_INFORMATION)
    
#------------------------

app = wx.App(False)
frame = Gogo(None)
frame.Show()
app.MainLoop()